/**
 * Created with IntelliJ IDEA.
 * Description:单链表的排序
 * User: wangxin
 * Date: 2024-10-25
 * Time: 23:23
 */
import java.util.*;


  class ListNode {
    int val;
    ListNode next = null;
    public ListNode(int val) {
      this.val = val;
    }
  }


public class test {
    public ListNode sortInList (ListNode head) {
        ArrayList<Integer> nums = new ArrayList();
        ListNode p = head;
        //遍历链表，将节点值加入数组
        while(p != null){
            nums.add(p.val);
            p = p.next;
        }
        p = head;
        //对数组元素排序
        Collections.sort(nums);
        //遍历数组
        for(int i = 0; i < nums.size(); i++){
            //将数组元素依次加入链表
            p.val = nums.get(i);
            p = p.next;
        }
        return head;
    }
}
